package com.yingjia.yingjiauniversity.utils;

import android.app.Activity;

import java.util.Stack;

/**
 * 应用程序Activity管理类：用于Activity管理和应用程序退出<br>
 * <p>
 * <b>创建时间</b> 2014-2-28
 *
 * @author kymjs (https://github.com/kymjs)
 * @version 1.1
 */
final public class ActivityStack {
    private static Stack<Activity> activityStack;
    private static ActivityStack instance;

    private ActivityStack() {
    }

    public static ActivityStack getInstance() {
        if (instance == null) {
            synchronized (ActivityStack.class) {
                if (instance == null) {
                    instance = new ActivityStack();
                    activityStack = new Stack<Activity>();
                }
            }
        }

        return instance;
    }

    /**
     * 获取当前Activity栈中元素个数
     */
    public int getCount() {
        return activityStack.size();
    }

    /**
     * 添加Activity到栈
     */
    public <T extends Activity> void addActivity(T activity) {
        activityStack.add(activity);
    }

    /**
     * 获取当前Activity（栈顶Activity）
     */
    public Activity topActivity() {
        if (activityStack == null) {
            throw new NullPointerException(
                    "Activity stack is Null,your Activity must extend BaseActivity");
        }
        if (activityStack.isEmpty()) {
            return null;
        }
        Activity activity = activityStack.lastElement();
        return (Activity) activity;
    }

    /**
     * 获取当前Activity（栈顶Activity） 没有找到则返回null
     */
    public <T extends Activity> Activity findActivity(Class<T> cls) {
        Activity activity = null;
        for (Activity aty : activityStack) {
            if (aty.getClass().equals(cls)) {
                activity = aty;
                break;
            }
        }
        return (Activity) activity;
    }

    /**
     * 结束当前Activity（栈顶Activity）
     */
    public void finishActivity() {
        Activity activity = activityStack.lastElement();
        finishActivity((Activity) activity);
    }

    /**
     * 结束指定的Activity(重载)
     */
    public <T extends Activity> void finishActivity(T activity) {
        if (activity != null) {
            activityStack.remove(activity);
            // activity.finish();//此处不用finish
            activity = null;
        }
    }

    /**
     * 结束指定的Activity(重载)
     */
    public <T extends Activity> void finishActivity(Class<T> cls) {
        for (Activity activity : activityStack) {
            if (activity.getClass().equals(cls)) {
                finishActivity((Activity) activity);
            }
        }
    }

    /**
     * 关闭除了指定activity以外的全部activity 如果cls不存在于栈中，则栈全部清空
     *
     * @param cls
     */
    public void finishOthersActivity(Class<?> cls) {
        for (Activity activity : activityStack) {
            if (!(activity.getClass().equals(cls))) {
                finishActivity((Activity) activity);
            }
        }
    }

    /**
     * 结束所有Activity
     */
    public void finishAllActivity() {
        for (int i = 0, size = activityStack.size(); i < size; i++) {
            if (null != activityStack.get(i)) {
                ((Activity) activityStack.get(i)).finish();
            }
        }
        activityStack.clear();
    }


    /**
     * 是否存在某个activity实例
     *
     * @param activityClass
     * @param <T>
     * @return
     */
    public <T extends Activity> boolean existsActivity(Class<T> activityClass) {
        for (int i = 0; i < activityStack.size(); i++) {
            if (activityStack.get(i).getClass() == activityClass) {
                return true;
            }
        }

        return false;
    }

    /**
     * 关闭某个activity及上面所有的activity
     *
     * @param exitActivityClass
     */
    public <T extends Activity> boolean finishActivityWhileTop(Class<T> exitActivityClass) {
        for (int i = 0; i < activityStack.size(); i++) {
            if (activityStack.get(i).getClass() == exitActivityClass) {
                while (activityStack.size() != i) {
                    activityStack.get(i).finish();
                    activityStack.remove(i);
                }
                return true;
            }
        }

        return false;
    }

    /**
     * 得到某个个activity实例的索引
     *
     * @param activityClass
     * @param <T>
     * @return
     */
    public <T extends Activity> int indexOfActivity(Class<T> activityClass) {
        for (int i = 0; i < activityStack.size(); i++) {
            if (activityStack.get(i).getClass() == activityClass) {
                return i;
            }
        }

        return -1;
    }

    /**
     * 得到某个个activity实例的索引
     *
     * @param activityClass
     * @param <T>
     * @return
     */
    public <T extends Activity> int lastIndexOfActivity(Class<T> activityClass) {
        for (int i = activityStack.size() - 1; i >= 0; i++) {
            if (activityStack.get(i).getClass() == activityClass) {
                return i;
            }
        }

        return -1;
    }

    /**
     * 获取当前所有的activity
     *
     * @return
     */
    public Stack<Activity> getStackActivities() {
        return activityStack;
    }

}